var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var Base64util = require("../../utils/base64.js");
var WxParse = require("../../lib/wxParse/wxParse.js")

var app = getApp();

Page({
  data: {
    sceneId: 0,
    scene: "",
    tickets: [],
    images: [],
    coverImage: "",
    imageNumber: 1,
    notes: [],
    winWidth: 0,
    winHeight: 0,
    // tab切换  
    currentTab: 0
  },
  getData: function () {
    let that = this;
    util.request(api.SceneDetail + that.data.sceneId).then(function (res) {
      if (res.code === '200') {
        console.log(res.datas.images)
        that.setData({
          tickets: res.datas.tickets,
          images: res.datas.images,
          scene: res.datas.scene,
          notes: res.datas.notes,
          coverImage: res.datas.coverImage,
          imageNumber: res.datas.images.length
        });

        wx.setNavigationBarTitle({
          //页面标题为路由参数
          title: res.datas.scene.sceneName
        })
        WxParse.wxParse('content', 'html', Base64util.Base64.decode(res.datas.scene.info, "utf-8"), that, 5);
      }
    });
  },

  onLoad: function (options) {
    var that = this;
    //初始化
    app.init(options, that);
    that.setData({
      sceneId: options.id//options为页面路由过程中传递的参数
    })
    // 页面初始化 options为页面跳转所带来的参数
    this.getData();
    /** 
      * 获取系统信息 
      */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    app.getShareInfo(this);
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  previewImage: function () {
    var that = this;
    if (that.data.images.length > 1) {

    wx.previewImage({
      current: 1, // 当前显示图片的http链接  
      urls: that.data.images// 需要预览的图片http链接列表  
    })}
  },
  /** 
     * 滑动切换tab 
     */
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });

  },
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  fnPhoneCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: '4000282666',
    })
  },
  fnShare: function (e) {
    var path = "/pages/sceneDetail/sceninfo"
    var sceneId = this.data.sceneId;
    var imgUrl = this.data.scene.path;
    var price = e.currentTarget.dataset.price;
    var marketPrice = e.currentTarget.dataset.marketprice;
    app.fnShareGoods(path, sceneId, imgUrl, this.data.scene.sceneName, "scene", this.data.scene.address, price,marketPrice);
  },
  fnOrder: function (e) {
    var ticket = e.currentTarget.dataset.item;
    var type = "scene";
    var id = ticket.id;
    var url = "/pages/preOrder/sceneOrder/scenorder?type=" + type +
      "&id=" + id;
    wx.navigateTo({
      url: url,
    });
  }, openMap: function (e) {
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        var latitude = that.data.scene.latitude
        var longitude = that.data.scene.longitude
        console.log("longitude", longitude);
        console.log("latitude", latitude);
        wx.openLocation({
          latitude: parseFloat(latitude),
          longitude: parseFloat(longitude),
          scale: 28
        })
      }
    })
  },
  fnToIndex: function () {
    wx.switchTab({
      url: '/pages/index/index-new',
    })
  },
  fnCall: function (e) {
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  }
  , fnDetail: function (e) {
    var content = e.currentTarget.dataset.content;
    wx.navigateTo({
      url: '/pages/shopdetail/index?content=' +content,
    })
  }
})